package br.com.managerprojectplugin.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.EditorPart;

import br.com.managerprojectplugin.model.Usecase;

public class EditUseCaseEditor extends EditorPart implements IEditorInput {
	public static final String ID = "EditUseCaseEditor"; //$NON-NLS-1$
	private FormToolkit formToolkit;
	private ScrolledForm scrolledForm;
	private Usecase uc;
	
	public EditUseCaseEditor() {
		// TODO Auto-generated constructor stub
	}
	
	public EditUseCaseEditor(Usecase usecase) {
		this.setUc(usecase);
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
		uc=((EditUseCaseEditor) input).getUc();
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
	
	

	@Override
	public void createPartControl(Composite parent) {
		formToolkit = new FormToolkit(parent.getDisplay());
		scrolledForm = formToolkit.createScrolledForm(parent);
		scrolledForm.getBody().setLayout(new GridLayout(2, false));
		Section section = formToolkit.createSection(scrolledForm.getBody(), Section.TITLE_BAR);
		section.setText(Messages.EditUseCaseEditor_editusecase);
		section.setLayoutData(getSectionData());
		section.setLayout(new GridLayout(2, false));
		Composite sectionComposite = formToolkit.createComposite(section);
		sectionComposite.setLayout(new GridLayout(2, false));
		section.setClient(sectionComposite);
		
		
		Label useCaseLabel = new Label(sectionComposite, SWT.LEFT);
		useCaseLabel.setText(Messages.EditUseCaseEditor_usecase);
		Text useCaseText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getNome()!=null){
			useCaseText.setText(getUc().getNome());
		}
		useCaseText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		Label nameLabel = new Label(sectionComposite, SWT.LEFT);
		nameLabel.setText(Messages.EditUseCaseEditor_projectname);
		Text nameText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getNomeProjeto()!=null){
			nameText.setText(getUc().getNomeProjeto());
		}
		nameText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		Label descriptionLabel = new Label(sectionComposite, SWT.LEFT);
		descriptionLabel.setText(Messages.EditUseCaseEditor_description);
		Text descriptionText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getProjetoDesc()!=null){
			descriptionText.setText(getUc().getProjetoDesc());
		}
		descriptionText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		Label inicioLabel = new Label(sectionComposite, SWT.LEFT);
		inicioLabel.setText(Messages.EditUseCaseEditor_begin);
		Text inicioText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getInicio()!=null){
			inicioText.setText(getUc().getInicio());
		}
		inicioText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		Label terminoLabel = new Label(sectionComposite, SWT.LEFT);
		terminoLabel.setText(Messages.EditUseCaseEditor_end);
		Text terminoText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getTerminno()!=null){
			terminoText.setText(getUc().getTerminno());
		}
		terminoText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		Label estadoLabel = new Label(sectionComposite, SWT.LEFT);
		estadoLabel.setText(Messages.EditUseCaseEditor_status);
		Text estadoText = new Text(sectionComposite, SWT.BORDER);
		if(getUc().getEstado()!=null){
			estadoText.setText(getUc().getEstado());
		}
		estadoText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
			}
		});
		
		
		scrolledForm.reflow(true);
		
		
	}

	@Override
	public void setFocus() {
		scrolledForm.setFocus();
	}
	
	public GridData getSectionData(){
		GridData sectionData = new GridData();
		sectionData.horizontalAlignment = SWT.FILL;
		sectionData.verticalAlignment = SWT.FILL;
		sectionData.grabExcessVerticalSpace = true;
		sectionData.grabExcessHorizontalSpace = true;
		sectionData.horizontalSpan=2;
		return sectionData;
		
	}

	@Override
	public boolean exists() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IPersistableElement getPersistable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getToolTipText() {
		// TODO Auto-generated method stub
		return null;
	}

	public Usecase getUc() {
		return uc;
	}

	public void setUc(Usecase uc) {
		this.uc = uc;
	}

}
